package com.weilive.api.utils;

import java.io.ByteArrayInputStream;

import org.apache.commons.lang.StringUtils;

import com.ksyun.ks3.dto.CannedAccessControlList;
import com.ksyun.ks3.dto.ObjectMetadata;
import com.ksyun.ks3.exception.serviceside.NotFoundException;
import com.ksyun.ks3.service.Ks3;
import com.ksyun.ks3.service.Ks3Client;
import com.ksyun.ks3.service.request.HeadObjectRequest;
import com.ksyun.ks3.service.request.PutObjectRequest;
import com.weilive.api.ServerConfig;

/**
 * 
 * 金山数据操作接口
 * 
 * @author houzhiwei
 *
 */
public final class Ks3Util {

	private static final String BUCKEY_NAME = ServerConfig.getKs3BuckeyName();
	private static final String KS3_PHOTO_URL = ServerConfig.getKs3PhotoURL();

	private static final String ACCESSKEY = "wADcRzf96HvPLE3JhAcS";
	private static final String ACCESSKEY_SECRET = "QpZZ6LaPvuiS3/z33fjeb95NZ+Dcf/RKompw/nZK";

	/**
	 * 上传文件
	 * 
	 * @param images
	 * @param fileName
	 */
	public static final boolean uploadFile(byte[] file, String fileName) {
		if (file == null || file.length == 0) {
			return false;
		}
		Ks3 client = new Ks3Client(ACCESSKEY, ACCESSKEY_SECRET);

		ByteArrayInputStream in = new ByteArrayInputStream(file);
		ObjectMetadata data = new ObjectMetadata();
		data.setContentLength(file.length);

		PutObjectRequest request = new PutObjectRequest(BUCKEY_NAME, fileName, in, data);
		request.setCannedAcl(CannedAccessControlList.PublicReadWrite);

		try {
			client.putObject(request);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	private static final boolean objectExists(Ks3 client, String buckeyName, String fileName) {
		try {
			HeadObjectRequest request = new HeadObjectRequest(buckeyName, fileName);
			client.headObject(request);
			return Boolean.TRUE;
		} catch (NotFoundException e) {
			return Boolean.FALSE;
		}
	}

	/**
	 * 删除文件
	 * 
	 * @param fileName
	 */
	public static final void deleteFile(String fileName) {
		if (StringUtils.isBlank(fileName)) {
			return;
		}
		Ks3 client = new Ks3Client(ACCESSKEY, ACCESSKEY_SECRET);

		if (objectExists(client, BUCKEY_NAME, fileName)) {

			client.deleteObject(BUCKEY_NAME, fileName);
		}

	}

	public static final String getPhotoURL(String fileName) {

		return String.format(KS3_PHOTO_URL, BUCKEY_NAME, fileName);
	}

	public static final String getKs3PhotoKey(String url) {
		if (StringUtils.isBlank(url)) {
			return "";
		}
		try {
			String key = url.substring(url.lastIndexOf("/") + 1, url.length());
			return "photo/" + key;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	public static void main(String[] args) throws Exception {

		// byte[] b = toByteArray(new File("d:/image/test.jpg"));
		//
		String fileName = "test123";
		// uploadFile(b, fileName);
		//
		System.out.println(getPhotoURL(fileName));
	}
	//
	// private static byte[] toByteArray(File imageFile) throws Exception {
	// BufferedImage img = ImageIO.read(imageFile);
	// ByteArrayOutputStream buf = new ByteArrayOutputStream((int)
	// imageFile.length());
	// try {
	// ImageIO.write(img, "jpg", buf);
	// } catch (Exception e) {
	// e.printStackTrace();
	// return null;
	// }
	// return buf.toByteArray();
	// }
}
